<?php

////// get the child categories of a category and check current post belong to one of them
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
	function post_is_in_descendant_category( $cats, $_post = null ) {
		foreach ( (array) $cats as $cat ) {
			// get_term_children() accepts integer ID only
			$descendants = get_term_children( (int) $cat, 'category' );
			if ( $descendants && in_category( $descendants, $_post ) )
				return true;
		}
		return false;
	}
}
?>
<?php if ( have_posts() ) : ?>

    <div class="loop">
        <div id="loop" class="<?php if ($_COOKIE['mode'] == 'grid' ) echo 'grid'; else echo 'list'; ?> clear">
        <?php
            $postedon_data = array(
                'date' => get_option('unspoken_postedon_date'),
                'category' => get_option('unspoken_postedon_cat'),
                'comment' => get_option('unspoken_postedon_comm'),
                'author' => get_option('unspoken_postedon_author')
            );
            $i = 0; $j=0;
			$companies ="";			///// for storing all companies divs
			$products ="";			///// for storing all products divs
			$postCat="";
            while ( have_posts() ) : the_post();
				if (in_category( 12 ) || post_is_in_descendant_category( 12 )){		/////// check the post belong to companies
					$postCat="Company"; 
					$i++;		
				} else {
					$postCat="Product";
					$j++;
				}
				ob_start();	///////// capture the div bu buffering
        ?>
                <div id="post-<?php the_ID(); ?>" <?php post_class('clear'); ?>>
                    <!--<a href="<?php the_permalink(); ?>" class="post-thumb"><?php if ( has_post_thumbnail() ) the_post_thumbnail('general'); ?></a>-->
                    <!--<div class="post-meta"><?php if (function_exists('unspoken_posted_on')) unspoken_posted_on($postedon_data); ?></div>-->
                    <h2><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                    <p><?php the_excerpt(); ?></p>
                    <p><?php if ( in_category( 12 ) || post_is_in_descendant_category( 12 ) ) {
								echo "Belong to company";
						} ?></p>
                </div>
        <?php
            if ( $i % 2 == 0 || $j % 2 == 0 ) echo '<div class="clear"></div>';
			
			////// attaching the contents of buffer with correct varibale
			if($postCat=="Company") $companies.=ob_get_contents(); else $products.=ob_get_contents();
			ob_end_clean();	///////// cleaning the buffer
            endwhile; // end of the loop.
        ?>

			<!-- Placing All Products Here -->
			<div class="title">Products</div>
            <div class="content"><?php echo $products; ?></div>
			
            <div class="clear"></div>
            
            <!-- Placing All Companies Here -->
            <div class="title">Companies</div>
            <div class="content"><?php echo $companies; ?></div>



            <div class="grid-line"></div>
        </div><!-- #loop -->
    </div><!-- .loop -->

<?php else : ?>

    <div class="loop">
        <div id="post-0" class="post hentry error404 not-found clear">
            <h2><?php _e( 'Not found', 'unspoken' ); ?></h2>
            <p><?php _e( 'Apologies, but no results were found for the requested criteria. Perhaps searching will help find a related post.', 'unspoken' ); ?></p>
        </div><!-- #post-0 -->
    </div>

<?php endif; ?>
